package org.oos.atom.elements;

import static org.oos.model.UserAvatar.DEFAULT_AVATAR;

import java.io.Serializable;

import org.oos.model.UserAvatar;

import com.google.api.client.util.Key;

public class AuthorElement implements Cloneable, Serializable, Author {

	private static final long serialVersionUID = 5047139874992557054L;

	@Key("name")
	private String nick;

	@Key("oos:avatar")
	private String avatarUrl;
	private UserAvatar avatar;
	
	@Key("oos:slug")
	private String slug;
	
	public AuthorElement() {}
	
	public AuthorElement(Author author) {
		this.avatar = author.getAvatar();
		this.nick = author.getNick();
		this.slug = author.getSlug();
	}
	
	public AuthorElement(String slug, String nick, String avatarUrl) {
		this.slug = slug;
		this.nick = nick;
		this.avatarUrl = avatarUrl;
		this.avatar = new UserAvatar(avatarUrl);
	}

	@Override
	public String getNick() {
		return nick;
	}
	
	public void setNick(String nick) {
		this.nick = nick;
	}

	@Override
	public String getSlug() {
		return slug;
	}
	
	public void setSlug(String slug) {
		this.slug = slug;
	}

	@Override
	public UserAvatar getAvatar() {
		if (avatarUrl != null) {
			if (avatar == null) {
				avatar = new UserAvatar(avatarUrl);
			}
			return avatar;
		} else {
			return DEFAULT_AVATAR;
		}
	}
	
	public void setAvatar(UserAvatar avatar) {
		this.avatar = avatar;
	}

	@Override
	public String toString() {
		return nick;
	}

	@Override
	public AuthorElement clone() throws CloneNotSupportedException {
		return (AuthorElement) super.clone();
	}

}
